home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 022 / lemacs / fileio.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  128 lines

  1. /*
  2.  * The routines in this file read and write ASCII files from the disk. All of
  3.  * the knowledge about files are here. A better message writing scheme should
  4.  * be used.
  5.  */
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. FILE    *ffp;                           /* File pointer, all functions. */
  11.  
  12. /*
  13.  * Open a file for reading.
  14.  */
  15. ffropen(fn)
  16. char    *fn;
  17. {
  18.         if ((ffp=fopen(fn, "r")) == NULL)
  19.                 return (FIOFNF);
  20.         return (FIOSUC);
  21. }
  22.  
  23. /*
  24.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  25.  * (cannot create).
  26.  */
  27. ffwopen(fn)
  28. char    *fn;
  29. {
  30. #if     VMS
  31.         register int    fd;
  32.  
  33.         if ((fd=creat(fn, 0666, "rfm=var", "rat=cr")) < 0
  34.         || (ffp=fdopen(fd, "w")) == NULL) {
  35. #else
  36.         if ((ffp=fopen(fn, "w")) == NULL) {
  37. #endif
  38.                 mlwrite("Cannot open file for writing");
  39.                 return (FIOERR);
  40.         }
  41.         return (FIOSUC);
  42. }
  43.  
  44. /*
  45.  * Close a file. Should look at the status in all systems.
  46.  */
  47. ffclose()
  48. {
  49. #if    MSDOS
  50.     fputc(26, ffp);        /* add a ^Z at the end of the file */
  51. #endif
  52.     
  53. #if     V7 | (MSDOS & LATTICE)
  54.         if (fclose(ffp) != FALSE) {
  55.                 mlwrite("Error closing file");
  56.                 return(FIOERR);
  57.         }
  58.         return(FIOSUC);
  59. #else
  60.         fclose(ffp);
  61.         return (FIOSUC);
  62. #endif
  63. }
  64.  
  65. /*
  66.  * Write a line to the already opened file. The "buf" points to the buffer,
  67.  * and the "nbuf" is its length, less the free newline. Return the status.
  68.  * Check only at the newline.
  69.  */
  70. ffputline(buf, nbuf)
  71. char    buf[];
  72. {
  73.         register int    i;
  74.  
  75.         for (i = 0; i < nbuf; ++i)
  76.                 fputc(buf[i]&0xFF, ffp);
  77.  
  78.         fputc('\n', ffp);
  79.  
  80.         if (ferror(ffp)) {
  81.                 mlwrite("Write I/O error");
  82.                 return (FIOERR);
  83.         }
  84.  
  85.         return (FIOSUC);
  86. }
  87.  
  88. /*
  89.  * Read a line from a file, and store the bytes in the supplied buffer. The
  90.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  91.  * at the end of the file that don't have a newline present. Check for I/O
  92.  * errors too. Return status.
  93.  */
  94. ffgetline(buf, nbuf)
  95. register char   buf[];
  96. {
  97.         register int    c;
  98.         register int    i;
  99.  
  100.         i = 0;
  101.  
  102.         while ((c = fgetc(ffp)) != EOF && c != '\n') {
  103.                 if (i >= nbuf-2) {
  104.             buf[nbuf - 2] = c;    /* store last char read */
  105.             buf[nbuf - 1] = 0;    /* and terminate it */
  106.                         mlwrite("File has long line");
  107.                         return (FIOLNG);
  108.                 }
  109.                 buf[i++] = c;
  110.         }
  111.  
  112.         if (c == EOF) {
  113.                 if (ferror(ffp)) {
  114.                         mlwrite("File read error");
  115.                         return (FIOERR);
  116.                 }
  117.  
  118.                 if (i != 0) {
  119.                         mlwrite("File has funny line at EOF");
  120.                         return (FIOERR);
  121.                 }
  122.                 return (FIOEOF);
  123.         }
  124.  
  125.         buf[i] = 0;
  126.         return (FIOSUC);
  127. }
  128.